# 练习作用域之间的转换
a = 5

def sum():
    global a  #用 global 转换

    a = 4
    print(a)

sum()
print(a)


#用装饰器,输出函数的执行时间
import time
def clock_it_deco(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"{func.__name__} execute time: {format(end_time-start_time, '.2f')}s ")
        return result
    return wrapper

@ clock_it_deco
def fact(n): #fact() 阶乘的意思
    if n == 1:
        return 1
    else:
         return n * fact(n - 1)
print(fact(10))


# 用装饰器来为斐波那契函数添加缓存
def cache_deco(func):
    a = {}
    def wrapper(*args):
        if args not in a:
           a[args] = func(*args)

        return a[args]

    return wrapper


@ cache_deco   # 调用
def f(n):
    if n <= 1:
        return n
    else:
        return  f(n - 1) + f(n - 2)

for i in range(12):
    print(f(i))